package com.hna.eking.AirGisPlt.Utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class AutoPublish {
	private static String username = "admin";
	private static String pwd = "geoserver";
	private static String baseUrl = "http://10.70.94.96:8080";
	
	public static void doPost(String Url, String content) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(baseUrl + "/geoserver/j_spring_security_check");
		List<NameValuePair> formParams = new ArrayList<NameValuePair>();
		formParams.add(new BasicNameValuePair("username", username));
		formParams.add(new BasicNameValuePair("password", pwd));
		formParams.add(new BasicNameValuePair("_spring_security_remember_me", "false"));
		UrlEncodedFormEntity uefEntity = null;
		
		try {
			uefEntity = new UrlEncodedFormEntity(formParams, "UTF-8");  
			httpPost.setEntity(uefEntity);
			CloseableHttpResponse response = httpClient.execute(httpPost);			
			httpPost.releaseConnection();
		
			httpPost = new HttpPost("http://10.70.94.96:8080/geoserver/rest/workspaces/AutoTest/coveragestores/");
			httpPost.setHeader("Content-type", "text/xml");
			String putcontent = "<coverageStore><name>TiffStore</name><type>GeoTIFF</type><enabled>true</enabled>"
					+ "<workspace><id>AutoWorkSpace1234567</id><name>AutoTest</name></workspace>"
					+ "<__default>false</__default><url>file:///root/GISData/srtm_59_08.tif</url></coverageStore>";
			StringEntity entity = new StringEntity(putcontent); //不指定键值对 直接已流的形式发送xml内容
			httpPost.setEntity(entity);
			httpClient.execute(httpPost);
			httpPost.releaseConnection();
			
			httpPost = new HttpPost("http://10.70.94.96:8080/geoserver/rest/workspaces/AutoTest/coveragestores/TiffStore/coverages");
			httpPost.setHeader("Content-type", "text/xml");
			putcontent = "<coverage><nativeCoverageName>srtm_59_08</nativeCoverageName><name>srtm_59_08</name></coverage>";
			entity = new StringEntity(putcontent);
			httpPost.setEntity(entity);
			httpClient.execute(httpPost);
			httpPost.releaseConnection();
			
			Thread.sleep(500);
			
			httpPost = new HttpPost("http://10.70.94.96:8080/geoserver/rest/layers/srtm_59_08");
			httpPost.setHeader("Content-type", "text/xml");
			putcontent = "<layer><defaultStyle><name>dem</name></defaultStyle></layer>";
			entity = new StringEntity(putcontent); 
			httpPost.setEntity(entity);
			httpClient.execute(httpPost);
			httpPost.releaseConnection();
			
		} catch (Exception e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		AutoPublish.doPost("", "");
	}
}
